<?php
	$s1 = $_GET["s1"];
	$s2 = $_GET["s2"];
	$style = $_GET["calStyle"];
	$numStyle = $_GET["Style"];	
$nextStyle = $_GET["nextCalStyle"];
	$flag = $_GET["flag"];

if ($numStyle ==null)
	$numStyle=10;
	switch ($numStyle)
	{
	case 10:
		$tmps1=$s1;
		$tmps2=$s2 ;
		break ;
	case 16:
		$tmps1=hexdec($s1);
		$tmps2=hexdec($s2) ;
		break;
	case 8:
		$tmps1=octdec($s1);
		$tmps2=octdec($s2) ;
		break;
	case 2:
		$tmps1=bindec($s1);
		$tmps2=bindec($s2) ;
		break;		
	}
		
	if($style == 'Cong')
		$tmpresult = $tmps1 + $tmps2;
elseif($style == 'Tru')  
	$tmpresult = $tmps1 - $tmps2;
elseif ($style == 'Nhan')
	$tmpresult = $tmps1 * $tmps2;
elseif($style == 'Chia')
	$tmpresult = $tmps1 / $tmps2;
elseif($style == 'Can')
	$tmpresult = sqrt($tmps1); 	
		
switch ($numStyle)
{
	case 10:
		$result= $tmpresult;
		break ;
	case 16:
		$result=dechex ( $tmpresult);
		break;
	case 8:
		$result=decoct( $tmpresult);
		break;
	case 2:
		$result=decbin( $tmpresult);
		break;		
}
	
/*	
function base2dec($base,&$num)
{
	switch ($base)
	{
		case 'Hex':
			$num= hexdec($num);
			break;
		break;
		
		case 'Bin':
			$num= bindec($num);
			break;
		case 'Dec':
			$num=$num;
			break;	
		case 'Oct':
			$num = octdec($num);
			break;
	}
}		
	*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Calculator</title>
<link rel="Stylesheet" type="text/css" href="style/main.css" />
<script type="text/javascript" language="JavaScript1.2" src="js/cal.js"></script>
<script language="javascript" type="text/javascript">
function changeNumStyle(num){
	if(num==16)
	{
		document.cal.bt1.disabled=""; 
		document.cal.bt2.disabled="";
		document.cal.bt3.disabled="";
		document.cal.bt4.disabled="";
		document.cal.bt5.disabled="";
		document.cal.bt6.disabled="";
		document.cal.bt7.disabled="";
		document.cal.bt8.disabled="";
		document.cal.bt9.disabled="";
		document.cal.bt0.disabled="";
		document.cal.btA.disabled="";
		document.cal.btB.disabled="";
		document.cal.btC.disabled="";
		document.cal.btD.disabled="";
		document.cal.btE.disabled="";
		document.cal.btF.disabled="";
		document.cal.Style.value=16;
		document.cal.numStyle[1].checked=true;
	}
	else if((num==10)|| (num==null))
	{
		document.cal.bt1.disabled=""; 
		document.cal.bt2.disabled="";
		document.cal.bt3.disabled="";
		document.cal.bt4.disabled="";
		document.cal.bt5.disabled="";
		document.cal.bt6.disabled="";
		document.cal.bt7.disabled="";
		document.cal.bt8.disabled="";
		document.cal.bt9.disabled="";
		document.cal.bt0.disabled="";
		document.cal.btA.disabled="disabled";
		document.cal.btB.disabled="disabled";
		document.cal.btC.disabled="disabled";
		document.cal.btD.disabled="disabled";
		document.cal.btE.disabled="disabled";
		document.cal.btF.disabled="disabled";
		document.cal.Style.value=10;
		document.cal.numStyle[2].checked=true;
	}
	else if(num==8)
	{
		document.cal.bt1.disabled=""; 
		document.cal.bt2.disabled="";
		document.cal.bt3.disabled="";
		document.cal.bt4.disabled="";
		document.cal.bt5.disabled="";
		document.cal.bt6.disabled="";
		document.cal.bt7.disabled="";
		document.cal.bt8.disabled="disabled";
		document.cal.bt9.disabled="disabled";
		document.cal.bt0.disabled="";
		document.cal.btA.disabled="disabled";
		document.cal.btB.disabled="disabled";
		document.cal.btC.disabled="disabled";
		document.cal.btD.disabled="disabled";
		document.cal.btE.disabled="disabled";
		document.cal.btF.disabled="disabled";
		document.cal.Style.value=8;
		document.cal.numStyle[3].checked=true;
	}
	else if(num==2)
	{
		document.cal.bt1.disabled=""; 
		document.cal.bt2.disabled="disabled";
		document.cal.bt3.disabled="disabled";
		document.cal.bt4.disabled="disabled";
		document.cal.bt5.disabled="disabled";
		document.cal.bt6.disabled="disabled";
		document.cal.bt7.disabled="disabled";
		document.cal.bt8.disabled="disabled";
		document.cal.bt9.disabled="disabled";
		document.cal.bt0.disabled="";
		document.cal.btA.disabled="disabled";
		document.cal.btB.disabled="disabled";
		document.cal.btC.disabled="disabled";
		document.cal.btD.disabled="disabled";
		document.cal.btE.disabled="disabled";
		document.cal.btF.disabled="disabled";
		document.cal.Style.value=2;
		document.cal.numStyle[4].checked=true;
	}
}
//var flag = 0;
//$flagOperator = 0;
function addText(str){
	if(document.cal.flag.value =='0')
		
		if(document.cal.result.value.substring(0,1) == '0')			
			document.cal.result.value = str ;
		else
			document.cal.result.value += str ;
	else
	{
		clearText();	
		document.cal.result.value += str
		document.cal.flag.value = '0';
		
	}
}
function clearText(){
	document.cal.result.value ='';
}
function revertValue(){
	if(document.cal.result.value.substring(0,1) == '-')
	{
		document.cal.result.value = document.cal.result.value.substring(1);
		return;
	}
	document.cal.result.value = '-' + document.cal.result.value;
}
function addCallStyle(style){
	//alert(document.cal.s1.value);-->
	document.cal.flag.value = '1';
		//alert(document.cal.calStyle.value)-->
	if(document.cal.s1.value == '' || document.cal.s1.value == 0)
	{			
			document.cal.s1.value = document.cal.result.value;
			document.cal.calStyle.value = style + '';
			if(style == 'Can')
				document.cal.submit();	
	}
	else{
		document.cal.nextCalStyle.value = style ;
		document.cal.s2.value = document.cal.result.value;
		
		document.cal.submit();
	}	
}
</script>
</head>
<body onload="changeNumStyle(<?php echo($numStyle);?>);">
<center>

<form method="get" action="index.php" name="cal">
<input type="hidden" name="s1" id="s1" value="<?php echo($result);?>"/>
	<input type="hidden" name="s2" id="s2" value="0"/> 
	<input type="hidden" name="kq" id="kq" value="<?php echo($result);?>"/> 
	<input type="hidden" name="calStyle" id="calStyle" value="<?php echo($nextStyle);?>"/>
	<input type="hidden" name="nextCalStyle" id="nextCalStyle" value="" />
	<input type="hidden" name="Style" id="numStyle" value="<?php echo($numStyle);?>"  />
	<input type="hidden" name="flag" id="flag" value="<?php echo($flag);?>"/>
	
	<div>
		<table border="1">
			<tr>
				<td colspan="4">
					<input type="text" name="result" id="result" class="bt" value="<?php echo($result);?>">
				</td>
			</tr>
			<tr>
				<td colspan="4">
					<input type="radio" name="numStyle" id="numStyle1" onclick="changeNumStyle(16)"> Hex 
					<input type="radio" name="numStyle" id="numStyle2" onclick="changeNumStyle(10)" checked="checked"> Dec 
					<input type="radio" name="numStyle" id="numStyle3" onclick="changeNumStyle(8)"> Oct 
					<input type="radio" name="numStyle" id="numStyle4" onclick="changeNumStyle(2)"> Bin 
				</td>
			</tr>
			<tr>
				<td width="50" class="tbcol">
					<input type="button" name="bt7" id="bt7" class="bt" value="7" onclick="addText(7);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="bt8" id="bt8" class="bt" value="8"  onclick="addText(8);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="bt9" id="bt9" class="bt" value="9"  onclick="addText(9);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btCong" id="btCong" class="bt" value="+" onclick="addCallStyle('Cong');">
				</td>
			</tr>
			<tr>
				<td width="50" class="tbcol">
					<input type="button" name="bt4" id="bt4" class="bt" value="4"  onclick="addText(4);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="bt5" id="bt5" class="bt" value="5"  onclick="addText(5);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="bt6" id="bt6" class="bt" value="6"  onclick="addText(6);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btTru" id="btTru" class="bt" value="-" onclick="addCallStyle('Tru');">
				</td>
			</tr>
			<tr>
				<td width="50" class="tbcol">
					<input type="button" name="bt1" id="bt1" class="bt" value="1" onclick="addText(1);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="bt2" id="bt2" class="bt" value="2" onclick="addText(2);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="bt3" id="bt3" class="bt" value="3" onclick="addText(3);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btNhan" id="btNhan" class="bt" value="*" onclick="addCallStyle('Nhan');">
				</td>
			</tr>
			<tr>
				<td width="50" class="tbcol">
					<input type="button" name="bt0" id="bt0" class="bt" value="0" onclick="addText(0);">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btct" id="btct" class="bt" value="+/-" onclick="revertValue();">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btexe" id="btexe" class="bt" value="=" onclick="addCallStyle('Bang');">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btChia" id="btChia" class="bt" value="/" onclick="addCallStyle('Chia');">
				</td>
			</tr>
			<tr>
				<td width="50" class="tbcol">
					<input type="button" name="btA" id="btA" class="bt" value="A">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btB" id="btB" class="bt" value="B">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btC" id="btC" class="bt" value="C">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btCan" id="btCan" class="bt" value="sqrt" onclick="addCallStyle('Can');">
				</td>
			</tr>
			<tr>
				<td width="50" class="tbcol">
					<input type="button" name="btD" id="btD" class="bt" value="D">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btE" id="btE" class="bt" value="E">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btF" id="btF" class="bt" value="F">
				</td>
				<td width="50" class="tbcol">
					<input type="button" name="btCl" id="btCl" class="bt" value="C" style = "color: red;"onclick="clearText();">
				</td>
			</tr>
		</table>
	</div>
</form>

</center>
</body>
</html>